home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PACKET / PMPSRC11.ZIP / MISC.C < prev    next >
Text File  |  1991-07-30  |  3KB  |  166 lines

  1. /*
  2.     misc.c -- Random stuff
  3.  
  4.   Poor Man's Packet (PMP)
  5.   Copyright (c) 1991 by Andrew C. Payne    All Rights Reserved.
  6.  
  7.   Permission to use, copy, modify, and distribute this software and its
  8.   documentation without fee for NON-COMMERCIAL AMATEUR RADIO USE ONLY is hereby
  9.   granted, provided that the above copyright notice appear in all copies.
  10.   The author makes no representations about the suitability of this software
  11.   for any purpose.  It is provided "as is" without express or implied warranty.
  12.  
  13.     April, 1990
  14.     Andrew C. Payne
  15. */
  16.  
  17. /* ---- Includes ----- */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <ctype.h>
  21. #include <string.h>
  22. #include "pmp.h"
  23.  
  24. /* ----- String Subroutines ----- */
  25. /* sob(p)
  26.     Given a character pointer, returns a pointer with blanks skipped.
  27. */
  28. char *sob(char *s)
  29. {
  30.     while(*s && isspace(*s))
  31.         s++;
  32.  
  33.     return s;
  34. }
  35.  
  36. /* extract(s,p)
  37.     Extracts from S into P, converts to lowercase.
  38. */
  39. char *extract(char *s, char *p)
  40. {
  41. /* copy until hit a comma, end of string, or whitespace */
  42.     while(*s && !isspace(*s) && *s != ',')
  43.         *p++ = tolower(*s++);
  44.  
  45.     *p = '\0';
  46.  
  47. /* skip over commas */
  48.     while(*s == ',')
  49.         s++;
  50.  
  51.     return sob(s);
  52. }
  53.  
  54. /* convert(dest, src)
  55.     Given a string in the form:  "testing^M", copies the string to the
  56.     destination, converting all control characters to their ASCII
  57.     representations.
  58.  
  59.     Returns TRUE if error.
  60. */
  61. int convert(char *dest, char *src)
  62. {
  63.     int    c;
  64.  
  65.     while(*src && *src != '\n') {
  66.         if(*src != '^')
  67.             *dest++ = *src++;
  68.         else {
  69.             if(src[1] == '^')
  70.                 c = '^';
  71.             else {
  72.                 c = toupper(src[1]) - '@';
  73.                 if(c < 0 || c > ' ')
  74.                     return TRUE;
  75.             }
  76.             *dest++ = c;
  77.             src += 2;
  78.         }
  79.     }
  80.     *dest = '\0';
  81.     return FALSE;
  82. }
  83.  
  84. /* trim(s)
  85.     Trims trailing white space (including newlines) from a string.
  86. */
  87. void trim(char *s)
  88. {
  89.     char *p;
  90.  
  91. /* find end of string */
  92.     p = s;
  93.     while(*p)
  94.         p++;
  95.  
  96. /* move backwards */
  97.     while(p > s && isspace(p[-1]))
  98.         p--;
  99.  
  100.     *p = '\0';
  101. }
  102.  
  103. /* ----- Command Dispatcher ----- */
  104.  
  105. /* dispatch(s, funcs)
  106.     Given a command string and a list of functions, dispatches to
  107.     function handler with the remainder of the string.
  108.  
  109.     Returns FAIL if not found.
  110. */
  111. int dispatch(char *s, struct param_cmd *pcmds)
  112. {
  113.     char    *p;
  114.     char    c[80];
  115.  
  116.     p = extract(s,c);
  117.     while(pcmds->cmd != NULL) {
  118.         if(!strcmp(pcmds->cmd,c))
  119.             return (pcmds->handler)(p);
  120.         pcmds++;
  121.     }
  122.     return FAIL;
  123. }
  124.  
  125. /* ----- EOL Converstion ----- */
  126.  
  127. /* eol_out(conv,s,l)
  128.     Given a EOL convention code, a string and a length, converts a '\n'
  129.     termintated string to the appropriate convention.
  130. */
  131. void eol_out(int conv, char *s, int l)
  132. {
  133.     switch(conv) {
  134.         case EOL_CR:
  135.             while(l--) {
  136.                 if(*s == '\n')
  137.                     *s = '\r';
  138.                 s++;
  139.             }
  140.         case EOL_LF:
  141.         default:
  142.             /* no conversion */
  143.             return;
  144.     }
  145. }
  146.  
  147. /* eol_in(conv,s,l)
  148.     Given a EOL convention code, a string and a length, converts a string
  149.     with the appropriate EOL convention to a '\n' terminated string.
  150. */
  151. void eol_in(int conv, char *s, int l)
  152. {
  153.     switch(conv) {
  154.         case EOL_CR:
  155.             while(l--) {
  156.                 if(*s == '\r')
  157.                     *s = '\n';
  158.                 s++;
  159.             }
  160.         case EOL_LF:
  161.         default:
  162.             /* no conversion */
  163.             return;
  164.     }
  165. }
  166.